package com.example.administrator.liveapp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.bean.ArticleListBean;
import com.example.administrator.liveapp.utils.AppUrl;

import java.util.List;

/**
 * Created by Administrator on 2017/6/5 0005.
 */

public class ArticleRecyclerView3Adapter extends RecyclerView.Adapter<ArticleRecyclerView3Adapter.ViewHolder> implements View.OnClickListener {

    public Context context;
    public List<ArticleListBean.Article2Bean> article2;
    public ArticleRecyclerView3Adapter(Context context, List<ArticleListBean.Article2Bean> article2){
        this.article2 = article2;
        this.context = context;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.article_recyclerview3_item,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        context = parent.getContext();
        //将创建的View注册点击事件
        view.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if(article2.size()>0){
            holder.article_text.setText(article2.get(position).getArticletitle());
            holder.article_comments.setText(article2.get(position).getArticlecommcount());
            holder.article_watch_number.setText(article2.get(position).getArticleviewcount());
            holder.article_Thumb_up_number.setText(article2.get(position).getArticlebestcount());
//            holder.article_time.setText("2017-02-24");
            Glide.with(context).load(AppUrl.URL+article2.get(position).getArticleimgurl().get(0)).into(holder.article_picture);
            Glide.with(context).load(AppUrl.URL+article2.get(position).getArticleimgurl().get(1)).into(holder.article_picture2);
            Glide.with(context).load(AppUrl.URL+article2.get(position).getArticleimgurl().get(2)).into(holder.article_picture3);
            //将position保存在itemView的Tag中，以便点击时进行获取
            holder.itemView.setTag(position);
        }
    }

    @Override
    public int getItemCount() {
        if(article2==null){
            return 0;
        }else{
            return article2.size();
        }
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView article_text;
        public TextView article_comments;
        public TextView article_watch_number;
        public TextView article_Thumb_up_number;
        public ImageView article_picture;
        public ImageView article_picture2;
        public ImageView article_picture3;
        public ViewHolder(View itemView) {
            super(itemView);
            article_text = (TextView) itemView.findViewById(R.id.article_text);
            article_comments = (TextView) itemView.findViewById(R.id.article_comments);
            article_watch_number = (TextView) itemView.findViewById(R.id.article_watch_number);
            article_Thumb_up_number = (TextView) itemView.findViewById(R.id.article_Thumb_up_number);
//            article_time = (TextView) itemView.findViewById(R.id.article_time);
            article_picture = (ImageView) itemView.findViewById(R.id.article_picture);
            article_picture2 = (ImageView) itemView.findViewById(R.id.article_picture2);
            article_picture3 = (ImageView) itemView.findViewById(R.id.article_picture3);
        }
    }

    private OnItemClickListener mOnItemClickListener = null;

    public static interface OnItemClickListener {
        void onItemClick(View view , int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}
